package cn.chenxi.hadoopproject.monthWeather.service.impl;

import cn.chenxi.hadoopproject.monthWeather.mapper.MonthWeatherMapper;
import cn.chenxi.hadoopproject.monthWeather.pojo.dto.MonthWeatherQuery;
import cn.chenxi.hadoopproject.monthWeather.pojo.entity.MonthWeather;
import cn.chenxi.hadoopproject.monthWeather.pojo.vo.MonthWeatherVO;
import cn.chenxi.hadoopproject.monthWeather.service.MonthWeatherService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
@Slf4j
public class MonthWeatherServiceImpl implements MonthWeatherService {
    @Autowired
    private MonthWeatherMapper monthWeatherMapper;

    @Override
    public List<MonthWeatherVO> select(MonthWeatherQuery monthWeatherQuery) {
        log.debug("查询月天气，查询条件：{} + {} + {}", monthWeatherQuery.getCityName(), monthWeatherQuery.getYear(), monthWeatherQuery.getMonth());

        if (Integer.parseInt(monthWeatherQuery.getMonth()) < 10) {
            monthWeatherQuery.setMonth("0" + monthWeatherQuery.getMonth());
        }

        List<MonthWeather> monthWeatherList = monthWeatherMapper.select(monthWeatherQuery);
        List<MonthWeatherVO> monthWeatherVOS = new ArrayList<>();

        for (MonthWeather monthWeather : monthWeatherList) {
            MonthWeatherVO monthWeatherVO = new MonthWeatherVO();
            monthWeatherVO.setWeather(monthWeather.getWeather());
            monthWeatherVO.setWeatherExtent(monthWeather.getWeatherExtent());
            monthWeatherVOS.add(monthWeatherVO);
        }
        return monthWeatherVOS;
    }
}
